package root.electronclass.electronclass.grade.homework

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.BasicDialogFragment
import root.electronclass.electronclass.common.base.ViewBindingFragment.Companion.EXTRA_DATA
import root.electronclass.electronclass.databinding.FragmentClassCurriculumSignDialogBinding
import root.electronclass.electronclass.databinding.FragmentClassHomeworkSubmitDialogItemBinding
import root.electronclass.electronclass.ext.startFragment
import root.electronclass.electronclass.grade.model.ClassHomeworkItem

/**
 * 选择作业弹窗
 * @author Lokiy
 * @date 2023/3/27 23:00
 */
class ClassHomeworkSubmitDialog : BasicDialogFragment<FragmentClassCurriculumSignDialogBinding>() {
    private var selectPosition: Int = 0

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val homeworkItems: List<ClassHomeworkItem> = arguments?.getParcelableArrayList(EXTRA_DATA) ?: return
        binding.recycler.adapter = BasicAdapter<ClassHomeworkItem, FragmentClassHomeworkSubmitDialogItemBinding> { bindingHolder, item ->
            bindingHolder.binding.itemSubject.text = item.subject
            bindingHolder.binding.itemContent.text = item.name
            bindingHolder.binding.root.isActivated = selectPosition == bindingHolder.adapterPosition
            bindingHolder.binding.root.setOnClickListener {
                selectPosition = bindingHolder.adapterPosition
                notifyChanged(list)
            }
        }.apply {
            refresh(homeworkItems)
        }

        onOkClick = {
            startFragment(ClassSubmitHomeworkFragment::class.java, bundleOf(EXTRA_DATA to homeworkItems[selectPosition]))
            dismiss()
        }
    }
}